package com.promotores;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.promotores.util.Parametros;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class CreaTablas extends SQLiteOpenHelper{
	
	// All Static variables
	// Database Version
	private static final int DATABASE_VERSION = 1;

	// Database Name
	private static final String DATABASE_NAME = "3232f9d0089ark";

	// Tablas
	private static final String TABLE_TIPOEVENTO = "TipoEvento";
	private static final String TABLE_EVENTO = "Evento";
	private static final String TABLE_PROMOCION = "Promocion";
	private static final String TABLE_TIPOPROMOCION = "TipoPromocion";
	private static final String TABLE_PARAMETROS = "Parametro";
	private static final String TABLE_ZONAS = "Zona";
	private static final String TABLE_PROMOTOR = "Promotor";
	
	// Columnas en común
	private static final String KEY_CREATED_AT = "created_at";
	private static final String KEY_UPDATED_AT = "updated_at";
	private static final String KEY_FECHAINI = "FechaIni";
	private static final String KEY_FECHAFIN = "FechaFin";
	private static final String KEY_FLAG_SAVED = "flagSaved";
	private static final String KEY_DELETED = "deleted";
	private static final String KEY_NOMBRE = "Nombre";
	
	// TipoEvento Table Columns names
	private static final String KEY_ID = "id";
	private static final String KEY_TIPO_EVENTO = "tipoEvento";

	// Evento Table Columns names	
	private static final String KEY_DESC = "Descripcion";
	private static final String KEY_ID_TIPO_EVENTO = "idTipoEvento";
	private static final String KEY_ID_SUBTIPO_EVENTO = "idSubTipoEvento";
	private static final String KEY_PROMOTOR = "NomPromotor";
	private static final String KEY_ESTADO = "Estado";
	private static final String KEY_URL_IMAGEN = "Url";
	private static final String KEY_AMBIENTE ="Ambiente";
	
	// tipoPromocion Table Columns names
	private static final String KEY_TIPO_PROMOCION = "tipoPromocion";
	
	// Promocion Table Columns names
	private static final String KEY_ID_LOCAL = "idLocal";
	private static final String KEY_ID_TIPO_PROMOCION = "idTipoPromocion";
	private static final String KEY_ID_EVENTO = "idEvento";
	private static final String KEY_DESCUENTO = "descuento";
	private static final String KEY_DIAS = "dias";	

	//Parametro Table Column names
	private static final String KEY_LAST_UPDATE ="last_update";
	//private static final String KEY_CURRENT_USER = "current_user";
	
	//Zonas
	private static final String KEY_AFORO = "aforo";
	private static final String KEY_FARE = "fare";
	private static final String KEY_ID_ZONA = "idZona";
	private static final String KEY_DESCRIPCION = "descripcion";
	
	//Promotor
	private static final String KEY_APPATERNO = "apePaterno";
	private static final String KEY_APMATERNO = "apeMaterno";
	private static final String KEY_EMAIL = "email";
	
	
	public CreaTablas(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	// Creating Tables
	@Override
	public void onCreate(SQLiteDatabase db) {
		String CREATE_TIPOEVENTO_TABLE = "CREATE TABLE " + TABLE_TIPOEVENTO 
				+ "(" + KEY_ID + " INTEGER PRIMARY KEY, " 
				+ KEY_TIPO_EVENTO + " TEXT, "
				+ KEY_FLAG_SAVED + " TEXT, "
				+ KEY_DELETED + " TEXT, "
                + KEY_CREATED_AT + " TEXT, " 
				+ KEY_UPDATED_AT + " TEXT" + ")";
		db.execSQL(CREATE_TIPOEVENTO_TABLE);

		String CREATE_EVENTO_TABLE = "CREATE TABLE " + TABLE_EVENTO + "("
				+ KEY_ID + " INTEGER PRIMARY KEY, " 
				+ KEY_NOMBRE + " TEXT, " 
				+ KEY_DESC + " TEXT, "
				+ KEY_ID_TIPO_EVENTO + " INTEGER, "
				+ KEY_FECHAINI + " TEXT, " 
				+ KEY_FECHAFIN + " TEXT, "
				+ KEY_ESTADO + " TEXT, "
				+ KEY_URL_IMAGEN + " VARCHAR(2083), "
				+ KEY_PROMOTOR + " TEXT, "
				+ KEY_APPATERNO + " TEXT, "
				+ KEY_APMATERNO + " TEXT, "
				+ KEY_EMAIL + " TEXT, "
				+ KEY_AMBIENTE + " TEXT, "
				+ KEY_FLAG_SAVED + " TEXT, "
				+ KEY_DELETED + " TEXT, "
                + KEY_CREATED_AT + " TEXT, " 
				+ KEY_UPDATED_AT + " TEXT, " 
                + KEY_ID_SUBTIPO_EVENTO + " TEXT)";
		db.execSQL(CREATE_EVENTO_TABLE);
		
		String CREATE_PROMOCION_TABLE = "CREATE TABLE " + TABLE_PROMOCION + "("
				+ KEY_ID + " INTEGER, " 
				+ KEY_ID_LOCAL + " INTEGER PRIMARY KEY, "
				+ KEY_ID_TIPO_PROMOCION + " INTEGER, "
				+ KEY_ID_EVENTO +  " INTEGER, " 
				+ KEY_FECHAINI + " TEXT, " 
				+ KEY_FECHAFIN + " TEXT, " 
				+ KEY_DESCUENTO + " DOUBLE, " 
				+ KEY_DIAS + " TEXT, "
				+ KEY_FLAG_SAVED + " TEXT, "
				+ KEY_DELETED + " TEXT, "
				+ KEY_CREATED_AT + " TEXT, " 
				+ KEY_UPDATED_AT + " TEXT" + ")";
		db.execSQL(CREATE_PROMOCION_TABLE);
		
		String CREATE_TIPOPROMOCION_TABLE = "CREATE TABLE " + TABLE_TIPOPROMOCION + "("
				+ KEY_ID + " INTEGER PRIMARY KEY, " 
				+ KEY_TIPO_PROMOCION + " TEXT, "
				+ KEY_FLAG_SAVED + " TEXT, "
				+ KEY_DELETED + " TEXT, "
				+ KEY_CREATED_AT + " TEXT, " 
				+ KEY_UPDATED_AT + " TEXT" + ")";
		db.execSQL(CREATE_TIPOPROMOCION_TABLE);
		
		String CREATE_PARAMETROS_TABLE = "CREATE TABLE " + TABLE_PARAMETROS + "("
				+ KEY_ID + " INTEGER PRIMARY KEY, "
				+ KEY_LAST_UPDATE + " TEXT)";				
		db.execSQL(CREATE_PARAMETROS_TABLE);

		String CREATE_ZONAS_TABLE = "CREATE TABLE " + TABLE_ZONAS + "("
				+ KEY_ID_EVENTO +  " INTEGER, "
				+ KEY_ID_ZONA +  " INTEGER, "
				+ KEY_NOMBRE +  " TEXT, "
				+ KEY_DESCRIPCION +  " TEXT, "
				+ KEY_FARE + " DOUBLE, " 
				+ KEY_AFORO + " INTEGER, "
				+ KEY_FLAG_SAVED + " TEXT, "
				+ KEY_DELETED + " TEXT, "
				+ KEY_CREATED_AT + " TEXT, " 
				+ KEY_UPDATED_AT + " TEXT, " 
				+ "PRIMARY KEY (" + KEY_ID_EVENTO + ", "+ KEY_ID_ZONA +"))";				
		db.execSQL(CREATE_ZONAS_TABLE);
		
		String CREATE_PROMOTOR_TABLE = "CREATE TABLE " + TABLE_PROMOTOR + "("
				+ KEY_ID + " INTEGER PRIMARY KEY, "
				+ KEY_ID_EVENTO +  " INTEGER, "
				+ KEY_NOMBRE + " TEXT, "
				+ KEY_APPATERNO + " TEXT, "
				+ KEY_APMATERNO + " TEXT, "
				+ KEY_EMAIL + " TEXT, "
				+ KEY_FLAG_SAVED + " TEXT, "
				+ KEY_DELETED + " TEXT, "
				+ KEY_CREATED_AT + " TEXT, " 
				+ KEY_UPDATED_AT + " TEXT" + ")";;				
		db.execSQL(CREATE_PROMOTOR_TABLE);

	}

	// Upgrading database
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_TIPOEVENTO);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_EVENTO);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROMOCION);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_TIPOPROMOCION);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_PARAMETROS);
		onCreate(db);
	}	
	
	public void insertLastUpdate(){
		SQLiteDatabase db = this.getWritableDatabase();
		Date fecha = new Date();
		java.sql.Date fechaSQL = new java.sql.Date(fecha.getTime());
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
		String strFecha = dateFormat.format(fechaSQL);
		ContentValues values = new ContentValues();
		values.put("last_updated", strFecha);
		// Inserting Row
		db.insert(TABLE_PARAMETROS, null, values);
		db.close(); // Closing database connection
	}
}
